/**
 *  @param { Function } 节流函数
 */
function throttle(fn, interval) {
	let enterTime = 0 // 触发的时间
	let gapTime = interval || 300 // 间隔时间，如果interval不传，则默认300ms
	return function() {
		let context = this;
		let backTime = new Date() // 第一次函数return即触发的时间
		if (backTime - enterTime > gapTime) {
			fn.call(context, ...arguments)
			enterTime = backTime // 赋值给第一次触发的时间，这样就保存了第二次触发的时间
		} else {
			// uni.showToast({
			// 	title: '亲!您点击的太快了呢',
			// 	icon: 'none'
			// })
		}
	}
}

/**
 * @param { Function } debounce 防抖函数
 */
function debounce(func, wait, immediately) {
	let timer
	let debounced = function(...args) {
		let result
		// 清除闹钟后，闹钟还是存在的
		if (timer) clearTimeout(timer)
		if (immediately) {
			let called = !timer
			timer = setTimeout(() => {
				timer = null
			}, wait)
			if (called) {
				result = func.apply(this, args)
			}
		} else {
			timer = setTimeout(() => {
				func.apply(this, args)
			}, wait)
		}
		return result
	}
	debounced.cancel = function() {
		clearTimeout(timer)
		timer = null
	}
	return debounced
}

function deepCopy(obj) {
	let newObj = obj.constructor === Array ? [] : {} //判断是深拷贝对象还是数组

	for (let i in obj) {
		if (typeof obj[i] === 'object') {
			newObj[i] = deepCopy(obj[i]) //  如果要拷贝的对象的属性依然是个复合类型，递归
		} else {
			newObj[i] = obj[i]
		}
	}

	return newObj
}

function formatDate(objDate, fmt) {

	var o = {
		"M+": objDate.getMonth() + 1, //月份
		"d+": objDate.getDate(), //日
		"h+": objDate.getHours() % 12 == 0 ? 12 : objDate.getHours() % 12, //小时
		"H+": objDate.getHours(), //小时
		"m+": objDate.getMinutes(), //分
		"s+": objDate.getSeconds(), //秒
		"q+": Math.floor((objDate.getMonth() + 3) / 3), //季度
		"S": objDate.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (objDate.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**
 *  @name 获取本地当前时间
 */
function getNowDate() {
	let y = new Date().getFullYear()
	let m = new Date().getMonth() + 1
	let d = new Date().getDate()

	let hour = new Date().getHours()
	let min = new Date().getMinutes()
	let sec = new Date().getSeconds()

	let date = `${y}-${ m < 10 ? '0' + m : m }-${d}`
	let time = `${hour < 10 ? '0' + hour : hour }:${min < 10 ? '0' + min : min }:${sec < 10 ? '0' + sec : sec }`

	return `${ date } ${ time}`
}
